package com.das.mechanic_base.adapter.maintain;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Space;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.MAINTAINBean;
import com.das.mechanic_base.bean.maintain.MainTainPlanBean;
import com.das.mechanic_base.bean.plan.PlanListBean;
import com.das.mechanic_base.utils.LanguageUtiles;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.kproduce.roundcorners.RoundImageView;

import java.util.List;

public class X3OrginFactoryPlanAdapter extends RecyclerView.Adapter<X3OrginFactoryPlanAdapter.MainTainHolder> {

    private List<MainTainPlanBean> mList;
    private Context mContext;

    public X3OrginFactoryPlanAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setDataList(List<MainTainPlanBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public List<MainTainPlanBean> getmList() {
        return mList;
    }

    @NonNull
    @Override
    public MainTainHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MainTainHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_orgin_factory_plan_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MainTainHolder holder, int position) {
        placeHolderViewVisiable(holder, position);
        final MainTainPlanBean bean = mList.get(position);
        X3GlideNewUtils.loadNormalImage(mContext, X3StringUtils.getImageUrl(bean.url),holder.riv_icon, R.mipmap.x3_car_icon_no);
        holder.tv_name.setText(bean.serviceBaseName);
        String unit = String.format(mContext.getString(R.string.x3_orgin_factory_plan_month_unit),getCurrentUnit(0, mContext));
        holder.tv_unit.setText(unit);
        modifyDate(holder,bean);
    }

    private void modifyDate(MainTainHolder holder,MainTainPlanBean bean){
        holder.edt_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(holder.edt_input.getText().toString())){return;}
                try {
                    int month = Integer.parseInt(holder.edt_input.getText().toString());
                    bean.lastReplaceTime = month;
                    String unit = String.format(mContext.getString(R.string.x3_orgin_factory_plan_month_unit), getCurrentUnit(month, mContext));
                    holder.tv_unit.setText(unit);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    private static String getCurrentUnit(long num, Context mContext) {
        if (LanguageUtiles.isDe()) {
            return (num == 1 || num == 0) ? "" : mContext.getString(R.string.x3_time_unit_have_e);
        }
        return (num == 1 || num == 0) ? "" : mContext.getString(R.string.x3_time_unit);
    }

    private void placeHolderViewVisiable(MainTainHolder holder, int pos) {
        if (pos == getItemCount() - 1) {
            holder.v_bottom.setVisibility(View.GONE);
        } else {
            holder.v_bottom.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MainTainHolder extends RecyclerView.ViewHolder {
        RoundImageView riv_icon;
        TextView tv_name;
        EditText edt_input;
        View v_bottom;
        TextView tv_unit;

        public MainTainHolder(@NonNull View itemView) {
            super(itemView);
            riv_icon = itemView.findViewById(R.id.riv_icon);
            tv_name = itemView.findViewById(R.id.tv_name);
            edt_input = itemView.findViewById(R.id.edt_input);
            v_bottom = itemView.findViewById(R.id.v_bottom);
            tv_unit = itemView.findViewById(R.id.tv_unit);
        }
    }
}
